package cn.webrx.coll;/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */


import java.util.ArrayList;
import java.util.List;

/**
 * <p>Project: jse2204 - cn.webrx.coll.ArrayList1
 * <p>Powered by webrx On 2022-09-16 17:12:02
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ArrayList1 {
    public static void main(String[] args) {
        //动态的数组,也有下标0 1 2 3
        //1-------------------------------- 实例化ArrayList
        List list = new ArrayList(5);
        list.add(5);
        list.add(5);
        list.add(5);

        //2-------------------------------- 添加元素
        list.add("60");
        list.add(5);
        list.add(.5);
        list.add("hello - java1.8");
        list.add(5);
        System.out.println(list.size());
        list.add(0, "java");
        System.out.println(list.get(4));
        list.addAll(2, List.of("aa", "bb", "cc"));
        System.out.println(list);
        System.out.println(list.size());

        //3-------------------------------- 删除元素
        //list.clear();
        System.out.println(list);

        //list.remove(500);
        Integer o = 5;

        //删除集合索引下标为5的那个元素
        //list.remove(5);

        //只删除集合中第一个出现的5对象
        //list.remove(o);

        //删除集合中所有的5元素对象
        //list.removeAll(List.of(5));

        //删除集合中所有的5元素对象
        //list.removeIf(e -> e == (Integer) 5);

        //删除集合中所有字符串对象
        //list.removeIf(e->e instanceof String && e.toString().contains("java"));

        System.out.println(list);

        //4--------------------------------修改元素
        list.set(1,500);
        System.out.println(list);


        //5--------------------------------查询判断元素
        System.out.println(list.contains(5000));
        System.out.println(list.indexOf(5000));

        //6--------------------------------遍历
        //(1)
        for(Object o2 : list){
            System.out.println(o2);
        }
        //(2)
        for (int i=0;i<list.size();i++) {
            System.out.println(list.get(i));
        }

        for (int i=list.size()-1;i>=0;i--) {
            System.out.println(list.get(i));
        }

        //System.out.println(list.size());

    }
}
